<template>
  <div class="home" id="home">
    <top-header :userName="userName" :infos="infos" :index="index"></top-header>
    <el-row class="el-content" v-loading="setPostLoding" element-loading-text="正在加载数据，请稍后......">
      <div class="col-left">
        <left-nav v-if="!setPostLoding" :data="menuList"></left-nav>
      </div>
      <div class="col-right">
        <right-tab></right-tab>
      </div>
    </el-row>
    <waterMark :screenWidth="screenWidth" :userInfo="info"></waterMark>
  </div>
</template>
<script>
  import { getInfo } from '../../config/proxy'
  import leftNav from '../../components/main_left'
  import topHeader from '../../components/header'
  import rightTab from '../../components/main_right'
  import waterMark from '../../components/waterMark'
  export default {
    data () {
      return {
        defaultActive: '',
        userId: '', // 用户id
        userName: '', // 用户名称
        userLogin: '', // 用户名称 英文
        menuList: [ // 菜单
          {
            menu_id: '1',
            menu_name: '营销管理系统'
          }
        ],
        or_id: '', // 组织岗位id
        info: '', // 用户信息
        infos: [], // 用户信息数组
        index: '',
        data: '',
        setPostLoding: false,
        screenWidth: document.body.clientWidth
      }
    },
    components: {
      leftNav,
      topHeader,
      rightTab,
      waterMark
    },
    methods: {
      getUserLogin () { // 获取登陆名称 英文的
        var _data = {
          adminId: this.userId
        }
        getInfo.adminInfo(_data, res => {
          console.log(res, 'admin')
          if (res.message === 'ok' && res.data !== null) {
            this.userLogin = res.data.user_login
            this.getRoleGroup()
          } else {
            this.message(res.message, 'error')
            this.setPostLoding = false
          }
        })
      },
      getRoleGroup () { // 获取权限列表
        var _data = {
          // user_login: this.userLogin
          userLogin: this.userLogin
        }
        getInfo.user(_data, res => {
          console.log(res)
          if (res.message === 'ok') {
            res.content.forEach(element => {
              if (element.branchName === '中商惠民') {
                element.branchName = '总公司'
              }
            })
            this.infos = res.content
            var postInfo = JSON.parse(window.localStorage.getItem('postInfo'))
            if (postInfo) {
              // console.log('存在')
              if (Number(this.userId) === Number(postInfo.userId)) {
                for (let i = 0; i < res.content.length; i++) {
                  const element = res.content[i]
                  // if (Number(element.branchId) === Number(postInfo.branchId) && Number(element.roleid) === Number(postInfo.roleid)) {
                  if (Number(element.or_id) === Number(postInfo.orId)) {
                    // console.log(element)
                    this.info = element
                    this.index = i
                    this.userName = element.user_name
                    this.or_id = element.or_id
                    window.sessionStorage.setItem('_userInfo', JSON.stringify(element))
                    this.getPerList()
                  }
                }
              } else {
                window.localStorage.removeItem('postInfo')
                for (let j = 0; j < res.content.length; j++) {
                  const element = res.content[j]
                  if (element.uor_ismain === 1) {
                    // console.log(element)
                    this.info = element
                    this.index = j
                    this.userName = element.user_name
                    this.or_id = element.or_id
                    window.sessionStorage.setItem('_userInfo', JSON.stringify(element))
                    this.getPerList()
                  }
                }
              }
              // console.log(this.index)
            } else {
              // console.log('不存在')
              for (let k = 0; k < res.content.length; k++) {
                const element = res.content[k]
                if (element.uor_ismain === 1) {
                  console.log(element)
                  this.info = element
                  this.index = k
                  this.userName = element.user_name
                  this.or_id = element.or_id
                  window.sessionStorage.setItem('_userInfo', JSON.stringify(element))
                  this.getPerList()
                }
              }
              // console.log(this.index)
            }
            let state = {
              infos: this.infos,
              info: this.info
              // per: res.result
            }
            this.$store.dispatch('SAVE_USERINFO', state)
          } else {
            this.$message({
              showClose: true,
              message: '请重新登录后再访问',
              type: 'error'
            })
          }
        })
      },
      getPerList () { // 获取菜单
        let _data = {
          admin_id: this.userId,
          post_id: this.or_id,
          sys_key: 21
        }
        console.log(_data)
        getInfo.per(_data, res => {
          console.log(res, 'per')
          // console.log(JSON.parse(res.result))
          if (res.result !== '') {
            this.menuList = JSON.parse(res.result)
            console.log(this.menuList)
            this.setPostLoding = false
            let state = {
              // infos: this.infos,
              // info: this.info,
              per: this.menuList
            }
            this.$store.dispatch('SAVE_USERINFO', state)
            // console.log(this.$store.state.user)
            window.sessionStorage.setItem('_menuList', JSON.stringify(this.menuList))
            this.$store.dispatch('menuList', this.menuList)
          } else {
            this.setPostLoding = false
            let state = {
              // infos: this.infos,
              // info: this.info,
              per: res.result
            }
            this.$store.dispatch('SAVE_USERINFO', state)
          }
        })
      },
      // isChrome () {
      //   let isChrome = navigator.userAgent.toLowerCase().match(/chrome/)
      //   if (!isChrome) {
      //     const h = this.$createElement
      //     this.$msgbox({
      //       title: '提示',
      //       message: h('p', { style: 'color: #f00; line-height: 24px' }, '您当前使用非Chrome 浏览器，推荐您安装 Chrome 浏览器以获得更好的体验。 '),
      //       showCancelButton: true,
      //       confirmButtonText: '下载Chrome',
      //       cancelButtonText: '取消'
      //     }).then(action => {
      //       window.location.href = 'http://sw.bos.baidu.com/sw-search-sp/software/e80aba170ee7c/ChromeStandalone_62.0.3202.94_Setup.exe'
      //     })
      //   }
      // },
      message (text, type) { // 消息提示
        this.$message({
          showClose: true,
          message: text,
          type: type
        })
      }
    },
    computed: {
      update_state: function () {
        return this.$store.state.updateState.setPostState
      }
    },
    mounted () {
      // 当窗口改变计算高度
      var me = this
      window.onresize = function () {
        window.screenWidth = document.body.clientWidth
        me.screenWidth = window.screenWidth
      }
    },
    created () {
      this.setPostLoding = true
      this.userId = this.$route.params.userName
      this.getUserLogin()
      // this.isChrome()
    },
    watch: {
      update_state (n, o) {
        this.setPostLoding = true
        this.getUserLogin()
      }
    }
  }
</script>
<style>
  .el-content {
    position: absolute;
    width: 100%;
    top: 50px;
    bottom:0;
  }
  .col-left{
    background: #eef1f6;
    height: 100%;
    width: 240px;
    float: left;
  }
  .col-right{
     height:100%;
     overflow: hidden;
  }
  .main-top{
    min-width: 1100px;
    height: 42px;
    background: #333333;
    position: relative;
  }
  .main-top .el-dropdown{
    position: fixed;
    right: 10px;
    display: none;
  }
  .el-dropdown-menu a{
    text-decoration: none;
    color: #333333;
  }
  .main-top .el-dropdown-link{
    color: #ffffff;
    padding: 10px 20px;
    text-align: center;
    display: inline-block;
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  .content{
    padding: 0 10px;
  }
  .fade-before-enter{
    animation: fadeInLeft .5s;
    -webkit-animation: fadeInLeft .5s;
  }
  .fade-leave-active {
    -webkit-animation: fadeOutRight .5s;
    animation: fadeOutRight .5s;
  }

  @keyframes fadeInLeft {
    from {
      opacity: 0;
      -webkit-transform: translate3d(-100%, 0, 0);
      transform: translate3d(-100%, 0, 0);
    }
    to {
      opacity: 1;
      -webkit-transform: none;
      transform: none;
    }
  }
  @keyframes fadeOutRight {
    from {
      opacity: 1;
    }
    to {
      opacity: 0;
      -webkit-transform: translate3d(100%, 0, 0);
      transform: translate3d(100%, 0, 0);
    }
  }
  @media screen and (max-width: 760px) {
    .col-left{
      width: 190px;
    }
    /*.main-top .el-dropdown{
      display: block;
    }*/
  }
  .line-check {
    width: 100%;
  }
  .line-check .el-form-item {
    margin-bottom: 15px;
  }
  .line-check .demo-form-inline div{
    display: inline-block;
  }
  .line-check .el-form {
    padding-top:15px;
    padding-bottom: 5px;
  }
  .mk-title{
    border: 1px solid #f2f2f2;
    border-width: 1px 0;
    position: relative;
    padding: 20px 0 8px 0;
    margin-bottom: 10px;
    text-align: right;
  }
  .mk-line-bottom{
    border-bottom: 1px solid #f2f2f2;
    position: relative;
    padding-top: 8px;
    padding-bottom: 8px;
    margin-bottom: 10px;
  }
  .line-check .el-dialog__body{
    padding-top:10px;
    padding-bottom: 10px;
  }
  .form-item{
    position: relative;
    border: 1px solid #eaeefb;
    border-radius: 4px;
    transition: .2s;
    padding: 20px;
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .form-item:first-child{
    margin-top: 10px;
    margin-bottom: 40px;
  }
  .form-item h4{
    width: 100px;
    position: absolute;
    top: -12px;
    margin: 0;
    color: #666;
    background: #fff;
    text-align: center;
  }
  .form-item:hover{
    box-shadow: 0 0 8px 0 rgba(232, 237, 250, .6), 0 2px 4px 0 rgba(232, 237, 250, .5);
  }
  .detail-info .el-row{
    margin-bottom: 15px;
  }
  .detail-info .el-row b{
    display: inline-block;
    width: 80px;
    text-align: right;
    margin-right: 10px;
    color: #666;
  }
  .detail-info .form-item{
    margin-top: 20px;
  }
  .el-form--inline .el-form-item__label{
    float: left;
  }
  .el-table th {
    background-color: #dfe6ec;
  }
  .el-table thead {
    color: black;
  }
</style>
